package org.ysu.shoeShopping.controller;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.ysu.shoeShopping.entity.Brands;
import org.ysu.shoeShopping.service.BrandsService;
import org.ysu.shoeShopping.tools.Result;

import java.util.List;

/**
 * 鞋子品牌管理控制器
 * 提供品牌信息的REST API接口，包括：
 * - 品牌信息的增删改查
 * - 品牌的上线下线管理
 * - 品牌状态的启用禁用控制
 *
 * @author makejava
 * @since 2025-09-13 17:10:38
 */
@RestController
@RequestMapping("brand")
public class BrandsController {
    /**
     * 品牌业务服务层
     */
    @Autowired
    private BrandsService brandsService;


    @PostMapping("/updateBrand")
    public Result<String>updateBrandInfo(@RequestBody Brands brands){
        Brands brands1=brandsService.queryById(brands.getBid());
        if (brands1 != null)
        {
            brandsService.update(brands);
            return Result.success("修改成功！");
        }
        else {
            return  Result.fail("3000","修改失败");
        }
    }
    @PostMapping("/updateBstate")
    public Result<String>updateBstate(Integer bid){
        Brands brands=brandsService.queryById(bid);
        if (brands.getBstate()== 0)
        {
            brands.setBstate(1);
            return Result.success("修改成功！");
        }
        else {
            brands.setBstate(0);
            return Result.success("修改成功！");
        }
    }
    @PostMapping("/addBrand")
    public Result<String>addBrand(@RequestBody Brands brands){
        Brands brands1=brandsService.insert(brands);
        if (brands1 != null)
        {
            return Result.success("添加成功");
        }else {
            return Result.fail("20000","添加成功");
        }

    }
    @PostMapping("/deleteBrand")
    public Result<String>deleteBrand(@RequestParam Integer bid){
        Brands brands=brandsService.queryById(bid);
        if (brands != null)
        {
            brandsService.deleteById(bid);
            return Result.success("删除成功！");
        }
        else {
            return Result.fail("5000","删除失败！");
        }
    }

    @GetMapping("/getAllBrands")
    public Result<List<Brands>>getAllBrands(){
        List<Brands>brandsList=brandsService.findAllBrands();
        if (brandsList != null)
        {
            return Result.success(brandsList);
        }else {
            return Result.fail("20000","查询失败");
        }

    }
    /**
     * 分页查询
     *
     * @param brands 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Brands>> queryByPage(Brands brands, PageRequest pageRequest) {
        return ResponseEntity.ok(this.brandsService.queryByPage(brands, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Brands> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.brandsService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param brands 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Brands> add(Brands brands) {
        return ResponseEntity.ok(this.brandsService.insert(brands));
    }

    /**
     * 编辑数据
     *
     * @param brands 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Brands> edit(Brands brands) {
        return ResponseEntity.ok(this.brandsService.update(brands));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.brandsService.deleteById(id));
    }

}

